package china.accountinghelper.activity;

import java.util.Calendar;

import com.xdroid.base.ActivityDialogBase;
import com.xdroid.util.RegexUtil;

import china.wc.tallyhelper.R;
import china.wc.tallyhelper.business.BusinessPerson;
import china.wc.tallyhelper.model.ModelPerson;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class ActivityPersonAddAndEdit extends ActivityDialogBase
{
    // ModelPerson的业务类
    private BusinessPerson mBusinessPerson;
    // ModelPerson对象
    private ModelPerson    mHandlingModel;

    private EditText       mDialogPersonAddAndEdit_EditText_Name;
    private RadioGroup     mDialogPersonAddAndEdit_RadioGroup_State;
    private Button         mDialogPersonAddAndEdit_Button_OK;
    private Button         mDialogPersonAddAndEdit_Button_Cancel;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_person_add_and_edit);

        initFields();
        initViews();
        initListeners();
        bindData();
        register();

        // 设置标题
        if (mHandlingModel.getID() <= 0) {
            setTitle(R.string.title_person_add);
        } else {
            setTitle(R.string.title_person_edit);
            setName();
            setState();
        }

    }

    @Override
    public void onViewClick(View v)
    {
        if (v.equals(mDialogPersonAddAndEdit_Button_OK)) {
            if (!nameIsOk()) {
                showToastShort(R.string.message_person_check_name);
            } else if (!stateIsOk()) {
                showToastShort(R.string.message_person_check_state);
            } else {
                setModelPerson();
                boolean _Result = mBusinessPerson.insertOrUpdateModel(mHandlingModel);
                setResultCode(_Result);
                finish();
            }

        } else if (v.equals(mDialogPersonAddAndEdit_Button_Cancel)) {
            setResult(RESULT_CANCELED);
            finish();
        }
    }

    @Override
    protected void initFields()
    {
        mBusinessPerson = new BusinessPerson();
        mHandlingModel = initHandlingModel();
    }

    @Override
    protected void initViews()
    {
        mDialogPersonAddAndEdit_EditText_Name = (EditText) findViewById(R.id.DialogPersonAddAndEdit_EditText_Name);
        mDialogPersonAddAndEdit_RadioGroup_State = (RadioGroup) findViewById(R.id.DialogPersonAddAndEdit_RadioGroup_State);
        mDialogPersonAddAndEdit_Button_OK = (Button) findViewById(R.id.DialogPersonAddAndEdit_Button_OK);
        mDialogPersonAddAndEdit_Button_Cancel = (Button) findViewById(R.id.DialogPersonAddAndEdit_Button_Cancel);
    }

    @Override
    protected void initListeners()
    {
        mDialogPersonAddAndEdit_Button_OK.setOnClickListener(this);
        mDialogPersonAddAndEdit_Button_Cancel.setOnClickListener(this);
    }

    @Override
    protected void bindData()
    {
    }

    @Override
    protected void register()
    {
    }

    /**
     * 若Intent中包含ModelPerson对象，则将该对象赋值给mHandlingModel。
     * 否则新建一个PersonId为0的ModelPerson对象，然后赋值给mHandlingModel。
     * 
     * @return mHandlingModel
     */
    private ModelPerson initHandlingModel()
    {
        ModelPerson _ModelPerson = (ModelPerson) getIntent().getSerializableExtra(ActivityPerson.EDITING_MODEL_OBJECT);

        if (_ModelPerson == null) {
            mHandlingModel = new ModelPerson();
            mHandlingModel.setID(0);
        } else {
            mHandlingModel = _ModelPerson;
        }

        return mHandlingModel;
    }

    /**
     * 将当前页面的Name和State，外加当前时间，赋值给mHandlingModel。
     * 
     * @return mHandlingModel
     */
    private ModelPerson setModelPerson()
    {
        mHandlingModel.setName(getName());
        mHandlingModel.setState(getState());
        mHandlingModel.setCreateTime(Calendar.getInstance());

        return mHandlingModel;
    }

    /**
     * 检查当前界面的Name是否合法
     * 
     * @return 合法返回true，不合法返回false。
     */
    private boolean nameIsOk()
    {
        String _Name = getName();
        return RegexUtil.isUserNameChn(_Name);
    }

    /**
     * 检查当前界面的State是否合法
     * 
     * @return 合法返回true，不合法返回false。
     */
    private boolean stateIsOk()
    {
        int _State = getState();

        if (_State == ModelPerson.STATE_ENABLED || _State == ModelPerson.STATE_DISABLED) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 从界面上的文本框中，得到人员姓名
     * 
     * @return 人员姓名
     */
    private String getName()
    {
        return mDialogPersonAddAndEdit_EditText_Name.getText().toString().trim();
    }

    /**
     * 将mHandlingModel的Name，显示到界面上的文本框中
     */
    private void setName()
    {
        if (mHandlingModel.getID() <= 0) {
            mDialogPersonAddAndEdit_EditText_Name.setText(null);
        } else {
            mDialogPersonAddAndEdit_EditText_Name.setText(mHandlingModel.getName());
        }

    }

    /**
     * 从界面上的单选框中，得到人员状态值。
     * 
     * @return 正常返回ModelPerson.STATE_ENABLED，禁用返回ModelPerson.STATE_DISABLED，其他返回-100。
     */
    private int getState()
    {
        int _StateId = mDialogPersonAddAndEdit_RadioGroup_State.getCheckedRadioButtonId();

        if (_StateId == R.id.DialogPersonAddAndEdit_RadioButton_StateNormal) {
            return ModelPerson.STATE_ENABLED;
        } else if (_StateId == R.id.DialogPersonAddAndEdit_RadioButton_StateDisabled) {
            return ModelPerson.STATE_DISABLED;
        } else {
            return -100;
        }

    }

    /**
     * 将mHandlingModel的State，显示到界面上的单选框中
     */
    private void setState()
    {
        int _State = mHandlingModel.getState();

        if (mHandlingModel.getID() <= 0) {
            mDialogPersonAddAndEdit_RadioGroup_State.clearCheck();
        } else {
            if (_State == ModelPerson.STATE_ENABLED) {
                mDialogPersonAddAndEdit_RadioGroup_State.check(R.id.DialogPersonAddAndEdit_RadioButton_StateNormal);
            } else if (_State == ModelPerson.STATE_DISABLED) {
                mDialogPersonAddAndEdit_RadioGroup_State.check(R.id.DialogPersonAddAndEdit_RadioButton_StateDisabled);
            }
        }

    }

    private void setResultCode(boolean pResult)
    {
        if (pResult == true) {
            setResult(ActivityPerson.RESULT_OK);
        } else {
            setResult(ActivityPerson.RESULT_ERR);
        }
    }

}
